package com.mikepenz.presenter.imp;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.amap.api.maps2d.model.LatLng;
import com.android.volley.VolleyError;
import com.mikepenz.app.R;
import com.mikepenz.app.activity.ProductActivity;
import com.mikepenz.bean.CarBean;
import com.mikepenz.bean.ParkBean;
import com.mikepenz.interactor.ParkInteractor;
import com.mikepenz.interactor.imp.ParkInteractorImp;
import com.mikepenz.presenter.ParkPresenter;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.CookicUntil;
import com.mikepenz.utils.SystemUtils;
import com.mikepenz.view.ParkView;
import com.quickdv.until.ViewHolder;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/16.
 */
public class ParkPresenterImp implements ParkPresenter, RequestListener<List<CarBean>> {

    final int UnWork = 0;
    final int Work = 1;
    final int Rest = 2;

    ParkInteractor interactor;
    ParkView view;
    Context c;
    List<CarBean> result;

    int currentPage = 1;

    public ParkPresenterImp(Context c) {
        this(c, (ParkView) c);
    }

    public ParkPresenterImp(Context c, ParkView view) {
        this.c = c;
        this.view = view;
        interactor = new ParkInteractorImp(c, this);
    }



    @Override
    public void getRestCar(String parkId) {
        if (parkId == null) return;
        String userId = CookicUntil.getUser().getUserId();
        if (userId == null) {
            view.showError("用户未登录");
            return;
        }
        view.showLoading();
        interactor.getRestCar(userId, parkId, Rest, currentPage);
    }

    @Override
    public void initParkInfo(ParkBean bean) {
        String address = bean.getAddress();
        LatLng latLng = null;
        if (bean.getLat() != 0 && bean.getLng() != 0) {
            latLng = new LatLng(bean.getLat(), bean.getLng());
            view.drawPostions(latLng);
        }
        if (address != null) view.setAddress(address, latLng);
        setCallPhone(bean);
    }

    protected void setCallPhone(final ParkBean bean) {

        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("tel:" + bean.getTel());
                Intent intent = new Intent(Intent.ACTION_DIAL, uri);
                c.startActivity(intent);
            }
        };
        view.setCallPhone(clickListener);

    }

    @Override
    public void onItemClick(int potions) {
        CarBean carBean = result.get(potions);
        Intent intent = new Intent(c, ProductActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("car", carBean);
        intent.putExtras(bundle);
        c.startActivity(intent);
    }

    @Override
    public void onImageLoad(View convertView, int position) {
        ImageView view = ViewHolder.get(convertView, R.id.park_car_pic);
        if (result.get(position).getPic()!=null && result.get(position).getPic().size()>0){
            Picasso.with(c).load(result.get(position).getPic().get(0)).centerCrop().resize(150,120).error(R.drawable.loadfailed).placeholder(R.drawable.loading).into(view);
        }
    }

    @Override
    public void Success(String tag, List<CarBean> result) {
        List<Map<String, Object>> bean = new ArrayList<>();
        for (CarBean carBean : result) {
            Map<String, Object> obj = SystemUtils.beanToMap(carBean);
            bean.add(obj);
        }
        view.dimissLoad();
        view.carList(bean, currentPage == 1);
        view.notifyDateChange();
        currentPage++;
        if (this.result == null)
            this.result = result;
        else
            this.result.addAll(result);
    }

    @Override
    public void Error(String tag, String error) {
        view.dimissLoad();
        view.showError(error);
    }

    @Override
    public void Exception(String tag, VolleyError error) {
        view.dimissLoad();
        view.showError(c.getString(R.string.system_error));
    }
}
